package com.lovi.service;


import com.lovi.dao.BillDao;
import com.lovi.dao.DiningTableDao;
import com.lovi.dao.MenuDao;
import com.lovi.dao.MultiTableDao;
import com.lovi.domain.Bill;
import com.lovi.domain.Menu;
import com.lovi.domain.MultiTableBean;
import com.lovi.utils.JDBCUtilsByDruid;
import com.lovi.utils.ScannerUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class BillService {
    private BillDao billDao = new BillDao();
    private MultiTableDao multiTableDao = new MultiTableDao();
    private DiningTableDao diningTableDao = new DiningTableDao();



    public boolean payBill(String empId) {
        //展示目前待结账的订单
        List<MultiTableBean> list = listCurrentUserBill(empId);
        System.out.println("以上是你的待结账订单，确定结账吗？(y/n),-1退出)");
        String key = ScannerUtils.readString(10);
        if (key.equals("-1")) return false;
        if (key.equals("y")) {
            Connection connection = null;
            //开启事务
            try {
                connection = JDBCUtilsByDruid.getConnection();
                connection.setAutoCommit(false);
                //结账成功，更新bill状态
                int i = billDao.update("update bill set state = '已结账' where billId = ? and state = '未结账'", empId);
                if(i>0){
                    //修改餐桌表
                    //获取要结账餐桌
                    Integer diningTableId = list.get(0).getDiningTableId();
                    int i1 = diningTableDao.update("update dinning_table " +
                            "set state = '空',orderId = null,orderName = null,orderTel = null " +
                            "where id = ?", diningTableId);
                    if(i1>0){
                        connection.commit();
                    }else {
                        //发现根本没有用..我们这里事务设置默认就是提交的所以前面虽然设置了flase，但是也是提交了
                        //todo: 待修复事务问题
                        connection.rollback();
                    }
                    return i1>0;
                }
                return false;
            } catch (SQLException e) {
                try {
                    connection.rollback();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                return false;
            } finally {
                JDBCUtilsByDruid.close(null, null, connection);
            }
        } else {
            return false;
        }

    }



    public boolean orderMenu(String billId, int orderMenuId, int orderNums, double orderMoney, int orderDiningTableId) {
        //计算金额 = 菜品价钱*菜品数量
        int i = billDao.update("insert into bill values\n" +
                "                (null,?,?,?,?,?,now(),'未结账')", billId, orderMenuId, orderNums, orderMoney, orderDiningTableId);
        return i > 0;
    }

    public List<MultiTableBean> listCurrentUserBill(String empId) {
        List<MultiTableBean> list = multiTableDao.list(MultiTableBean.class, "select bill.*, name ,price from bill " +
                "join menu on bill.menuId = menu.id " +
                "and diningTableId = ? and state = '未结账' ", empId);
        System.out.println("编号\t菜品编号\t\t菜品数量\t\t消费金额\t\t菜品桌号\t\t下单时间\t\t\t\t\t账单状态" +
                "\t\t菜品名称\t\t\t菜品单价");
        list.forEach(System.out::println);
        return list;

//        List<Bill> list = billDao.list(Bill.class, "select * from bill where billId = ? and state = '未结账'", empId);
//        System.out.println("编号\t菜品编号\t\t菜品数量\t\t菜品金额\t\t菜品桌号\t\t下单时间\t\t\t\t\t账单状态");
//        list.forEach(System.out::println);
    }
}
